﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class ChangePassword : TAM_SPORT.FormBase
    {
        private LoginDao loginDao;
        public ChangePassword()
        {
            InitializeComponent();
            lblUser.Text = Login.userName;
            txtFullName.Text = LoginDao.fullName;
            loginDao = new LoginDao();
        }

        private bool validate()
        {
            if (txtNewPass.Text.Trim() == ""
                || txtFullName.Text.Trim() == ""
                || txtCurrentPass.Text.Trim() == ""
                || txtCornfirmPass.Text.Trim() == "")
            {
                return false;
            }
            return true;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!validate())
            {
                Error er = new Error(UserConstant.INPUT_VALIDATE);
                er.ShowDialog();
                er.Dispose();
            }
            else
            {
                if (txtNewPass.Text.Trim() != txtCornfirmPass.Text.Trim())
                {
                    Error er = new Error(UserConstant.CHANGE_PASS_NO_MACTH);
                    er.ShowDialog();
                    er.Dispose();
                    txtCurrentPass.Text = "";
                    txtCornfirmPass.Text = "";
                    txtNewPass.Text = "";
                }
                else
                {
                    int result = loginDao.changePassword(Login.userName, txtCurrentPass.Text.Trim(),
                        txtNewPass.Text.Trim(), txtFullName.Text.Trim());
                    if (result == UserConstant.OK)
                    {
                        Result r = new Result(UserConstant.CHANGE_PASS_SUCCESS);
                        r.ShowDialog();
                        r.Dispose();
                        txtCurrentPass.Text = "";
                        txtCornfirmPass.Text = "";
                        txtNewPass.Text = "";
                    }
                    else if (result == UserConstant.UN_DEFINE)
                    {
                        Error er = new Error(UserConstant.CHANGE_PASS_UN);
                        er.ShowDialog();
                        er.Dispose();
                        txtCurrentPass.Text = "";
                        txtCornfirmPass.Text = "";
                        txtNewPass.Text = "";
                    }
                    else
                    {
                        Error er = new Error(UserConstant.CHANGE_PASS_FAIL);
                        er.ShowDialog();
                        er.Dispose();
                        txtCurrentPass.Text = "";
                        txtCornfirmPass.Text = "";
                        txtNewPass.Text = "";
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
